﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using HotelManagement.BL;
using System.Text.RegularExpressions;
using HotelManagement.Common;

namespace HotelManagement.PL
{
    public partial class PurchaseForm : Form
    {
        private GoodsBL goodsBL = new GoodsBL();

        private Goods m_GoodInfo = new Goods();
        private TradeType m_tradeType;
        private Trade m_TradeInfo = new Trade();

        public Goods GoodInfo
        {
            get { return m_GoodInfo; }
            set { m_GoodInfo = value; }
        }


        public PurchaseForm(TradeType tradeType)
        {
            InitializeComponent();
            m_tradeType = tradeType;
        }

        private void PurchaseForm_Load(object sender, EventArgs e)
        {
            if (m_tradeType == TradeType.Purchase)
            {
                this.Text = "进货管理";
                labTitle.Text = "进货管理";
                labCount.Text = "买入数量";
            }
            else 
            {
                this.Text = "出货管理";
                labTitle.Text = "出货管理";
                labCount.Text = "卖出数量";
            }

            if (m_GoodInfo != null) 
            {
                txtName.Text = m_GoodInfo.Name;
                txtName.Enabled = false;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string strIntPattern = "\\d+$" ;
            if(!Regex.IsMatch(txtCount.Text,strIntPattern))
            {
                MessageBox.Show("请输入数字", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            BuildTradeInfo();

            GoodInfo.Count = Convert.ToInt32(txtCount.Text);

            if (m_tradeType == TradeType.Purchase)
            {
                PurchaseGoods();
            }
            else 
            {
                SaleGoods();
            }
        }

        private void BuildTradeInfo()
        {
            m_TradeInfo.Type = Enum.GetName(typeof(TradeType), m_tradeType);
            m_TradeInfo.GoodsID = GoodInfo.ID;
            m_TradeInfo.GoodsName = GoodInfo.Name;
            m_TradeInfo.Count = Convert.ToInt32(txtCount.Text);
            if (m_tradeType == TradeType.Purchase)
            {
                m_TradeInfo.Cost = GoodInfo.PurchasePrice * m_TradeInfo.Count;
            }
            else
            {
                m_TradeInfo.Cost = GoodInfo.SalePrice * m_TradeInfo.Count;
            }
            m_TradeInfo.TradeTime = DateTime.Now;
        }

        private void PurchaseGoods() 
        {
            if (0 <= goodsBL.PurchaseGoods(GoodInfo, m_TradeInfo))
            {
                MessageBox.Show("操作成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                UpdateParent();
                this.Close();
            }
            else
            {
                MessageBox.Show("操作失败", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void SaleGoods() 
        {
            if (Convert.ToInt32(m_GoodInfo.Count) < Convert.ToInt32(txtCount.Text)) 
            {
                MessageBox.Show("库存数量少于卖出数量", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            if (0 <= goodsBL.SaleGoods(GoodInfo, m_TradeInfo))
            {
                MessageBox.Show("操作成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                UpdateParent();
                this.Close();
            }
            else
            {
                MessageBox.Show("操作失败", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }


        private void UpdateParent() 
        {
            SearchForm searchFrom = null;
            foreach (Form frm in this.Owner.MdiChildren) 
            {
                if (frm.GetType() == typeof(SearchForm)) 
                {
                    searchFrom = (SearchForm)frm;
                }
            }
            if (null != searchFrom) 
            {
                searchFrom.UpdateGridView();
            }
        }

        private void btnBack_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
